package com.fzb.ftp;

import java.io.File;
import java.util.Random;
import java.util.Scanner;

public class FtpReqeustImpl implements IFtpRequest {

	private static boolean wait = false;
	private FtpSession ftpSession;
	public FtpReqeustImpl(FtpSession ftpSession){
		this.ftpSession=ftpSession;
	}
	
	@Override
	public String inputUserName() {
		String userName;
		if (ftpSession.getFtpUser().getUserName() != null) {
			userName = ftpSession.getFtpUser().getUserName();
		} else {
			Scanner sc = new Scanner(System.in);
			System.out.println("input userName");
			userName = sc.nextLine();
			sc.close();
		}
		return "USER " + userName;
	}

	@Override
	public String inputPassword() {
		String password;
		if (ftpSession.getFtpUser().getPassword() != null) {
			password = ftpSession.getFtpUser().getPassword();
		} else {
			Scanner sc = new Scanner(System.in);
			System.out.println("input password");
			password = sc.nextLine();
			sc.close();
		}
		return "PASS " + password;
	}

	public String initCmd() {
		return "PORT 192,168,1,104," + new Random().nextInt(256) + ","
				+ new Random().nextInt(256);
	}

	@Override
	public void sendCmd() {
		Scanner sc = new Scanner(System.in);
		while (getlock()) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.print("> ");
		String cmd = sc.nextLine().trim();
		sendCmd(cmd);
	}
	
	public void sendCmd(String cmd){
		while (getlock()) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		if (cmd.startsWith("get ")) {
			String fileName = cmd.split(" ")[1];
			File saveFile;
			if (cmd.split(" ").length == 3) {
				saveFile = new File(cmd.split(" ")[2]);
			} else {
				saveFile = new File(PathKit.getRootPath() + "/" + fileName);
			}
			cmd = "RETR " + cmd.split(" ")[1];
			FtpSocketUtil.getProxy(ftpSession).getFile(saveFile);
			waitToUnLock();
		} else if (cmd.startsWith("put ")) {
			final File file = new File(cmd.split(" ")[1]);
			String putFile="";
			if (cmd.split(" ").length == 3) {
				putFile = cmd.split(" ")[2];
			} else {
				putFile = file.getName();
			}
			cmd = "STOR " + putFile;
			FtpSocketUtil.getProxy(ftpSession).putFile(file);
			waitToUnLock();
		} else if ("dir".equals(cmd)) {
			cmd = "LIST";
			FtpSocketUtil.getProxy(ftpSession).printOutStr();
			waitToUnLock();
		} else if (cmd.startsWith("cd ")) {
			cmd = "CWD " + cmd.split(" ")[1];
		} else if ("exit".endsWith(cmd)) {
			System.exit(-1);
		}
		ftpSession.sendMsg(cmd);
	}

	private void waitToUnLock(){
		while (!FtpReqeustImpl.getlock()) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public String changeToMasterModle() {
		return "PASV";
	}

	@Override
	public String changeToSYST() {
		return "TYPE I";
	}

	public synchronized static void lock() {
		wait = true;
	}

	public synchronized static boolean getlock() {
		return wait;
	}

	public synchronized static void unLock() {
		wait = false;
	}
}
